package mini.statement;

import java.util.List;
import java.util.LinkedList;

import mini.Env;
import mini.expr.Element;
import mini.expr.Expr;


public class AssignStatement extends Statement {

    private String name;
    private Expr expr;

    public AssignStatement(String id, Expr e) {
        name = id;
        expr = e;
    }

    public void eval(List<Env> listOfEnvs, LinkedList var) {
    	
        /* add name to the statementlist of variable names */
        if (!var.contains(name)) {
            var.add(name);
        //insert the variable with the specified name into the table with the 
        // evaluated result (which must be an integer
        }
        if( listOfEnvs != null && listOfEnvs.size() > 0 ) {
        	Env env = listOfEnvs.get(listOfEnvs.size()-1);
        	Element elem = expr.eval(listOfEnvs, var);
        	env.addToNametable( name, elem);
        } else {
        	System.err.println( "Internal error in Assignment Statement of " + name + "=" + expr );
        }
    }
}